<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/6
 * Time: 9:20
 */

namespace App\Http;


use App\Http\Controllers\Controller;

class BaseController extends Controller
{

    public function __construct()
    {
        $this->request = request();
    }

    function json_success($msg = 'OK', $data = [])
    {
        $return = [
            'status_code' => '200',
            'message' => $msg,
            'data' => $data,
            //'request_uri' => request('_cmd'),
        ];
        $this->clear_null($return['data']);
        return $return;
    }

    function json_error($msg, $data = [], $status_code = '500')
    {
        $msg = $msg ?: '服务器错误，请稍后重试';
        $return = [
            'status_code' => $status_code,
            'message' => $msg,
            'data' => $data,
            //'request_uri' => request('_cmd'),
        ];
        clear_null($return['data']);
        return $return;
    }

    function clear_null(&$data = '')
    {
        $data = json_decode(json_encode($data), true);
        if ($data === null || $data === false) {
            $data = '';
        }
        if (is_array($data) && !empty($data)) {
            foreach ($data as &$v) {
                if ($v === null || $v === false) {
                    $v = '';
                } else if (is_array($v)) {
                    $this->clear_null($v);
                } else if (is_string($v) && stripos($v, '.') === 0) {
                    $v = '0' . $v;
                }
            }
        }
    }

}